#version 450

layout(set = 0, binding = 0) uniform PerFrameUniformData {
    mat4 view;
    mat4 proj;
} pfuData;

layout(set = 1, binding = 2) uniform PerObjectUniformData {
    mat4 model;
    float isAnimated;
} pouData;

layout(set = 3, binding = 4) readonly buffer JointMatrices {
    mat4 jointMatrices[];
};


layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inTexCoord;
layout(location = 3) in vec3 inColor;
layout(location = 4) in uvec4 inJointIndices;
layout(location = 5) in vec4 inJointWeights;

layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec2 fragTexCoord;
layout(location = 2) out float fragDepth;

void main() {
    if (pouData.isAnimated > 0.5f) {
        mat4 skinMat = 
            inJointWeights.x * jointMatrices[inJointIndices.x] +
            inJointWeights.y * jointMatrices[inJointIndices.y] +
            inJointWeights.z * jointMatrices[inJointIndices.z] +
            inJointWeights.w * jointMatrices[inJointIndices.w];
        gl_Position = pfuData.proj * pfuData.view * pouData.model * skinMat * vec4(inPosition, 1.0);
    } else {
        gl_Position = pfuData.proj * pfuData.view * pouData.model * vec4(inPosition, 1.0);
    }

    fragDepth = gl_Position.z / gl_Position.w;
    // fragColor = inJointIndices.xyzw;// / 18.0;
    // fragColor = inJointWeights;
    // fragColor = uColor[0].color + uColor[1].color;
    fragTexCoord = vec2(inTexCoord.x, inTexCoord.y);
}